<template>
  <el-dialog
    title="批量设置部位设备"
    :visible.sync="visible"
    width="400px"
    :before-close="handleClose">
    <div class="dlg_wrap">
      <el-form class="rule-form" ref="form" inline :model="ruleForm">
        <el-form-item label="部位设备" prop="partModality" class="short-input">
          <el-select
            style="width:100%"
            size="small"
            v-model="ruleForm.partModality"
            placeholder="请选择"
            clearable
            filterable
            allow-create
            multiple>
            <el-option
              v-for="item in partModalityOptions"
              :key="item"
              :label="item"
              :value="item"
            ></el-option>
          </el-select>
        </el-form-item>
        <div class="form_tips">请确认，已选择<b class="fc_red">{{ids.length}}</b> 个部位，批量设置部位设备</div></el-form>
    </div>
    <span slot="footer" class="dialog-footer"><el-button @click="visible = false">取消</el-button><el-button type="primary" @click="sendSet">提交</el-button>
    </span>
  </el-dialog>
</template>
<script>
import { setpartmodality, getlistbypartmodality } from '@/api/api.js'
export default {
  props: {
    dialogVisiblePartModality: {
      type: Boolean,
      default: false
    },
    hospitalCode: {
      type: String,
      default: ''
    },
    ids: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      ruleForm: {
        partModality: ''
      },
      partModalityOptions: []
    }
  },
  watch: {
    visible(val) {
      if (val) {
        this.ruleForm.partModality = ''
        this.getPartModalityOptions()
      }
    }
  },
  computed: {
    visible: {
      get() {
        return this.dialogVisiblePartModality
      },
      set(val) {
        this.$emit('update:dialogVisiblePartModality', val)
      }
    }
  },
  methods: {
    sendSet() {
      const _ids = this.ids.map(item => item.id)
      const params = {
        hospitalCode: this.hospitalCode,
        ids: _ids,
        partModality: this.ruleForm.partModality.toString()
      }
      console.log('params', params)
      setpartmodality(params).then((res) => {
        if (res) {
          this.$message.success(res.message || '设置成功')
          this.visible = false
          this.$emit('refresh')
        }
      })
    },
    getPartModalityOptions() {
      /* 部位设备列表 */
      let params = {
        hospitalCode: this.hospitalCode
      }
      return new Promise(resolve => {
        getlistbypartmodality(params).then((res) => {
          if (res.code === 10000) {
            this.partModalityOptions = res.content.list
            resolve(res)
          }
        })
      })
    },
    handleClose() {
      this.visible = false
    }
  }
}
</script>
<style></style>
